package red.stu.mc.mod.codeblock.util.socket;

import java.io.IOException;
import java.net.Socket;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import net.minecraft.client.Minecraft;
import red.stu.mc.mod.codeblock.test.ScriptEngineBuffer;
import red.stu.mc.mod.codeblock.util.CodeBlockScriptEngineManager;
import red.stu.mc.mod.codeblock.util.MD5;
import red.stu.mc.mod.codeblock.util.socket.ServerSocket.ClientSocket;

public class Test {
	
	private ClientSocket testsocket;
	
	public void run() {
		Minecraft mc = Minecraft.getMinecraft();
		mc.getVersion();
		
		Minecraft.getMinecraft().player.capabilities.allowFlying = true;
		System.out.println("XXX");
		try {
			ServerSocket serverSocket = new ServerSocket(52000, new ServerSocketCallback() {
				
				@Override
				public void onClientJoin(ClientSocket socket) {
					System.out.println("客户端加入！");
					testsocket = socket;
				}
				
				@Override
				public void onClientData(ClientSocket socket, String data) {
					System.out.println("收到数据：" + data);
					try {
						// socket.writeEnd("f611391e32884fa69fc653133f250a14000000ER-System.Err.md5");
						socket.writeEnd(msg("000001", "IDE", "-v"));
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				
				@Override
				public boolean onBeforeClientJoin(Socket socket) {
					System.out.println("客户端即将加入：" + socket.getPort());
					return true;
				}

				@Override
				public void onClientExit(ClientSocket socket) {
					System.out.println("客户端离开！");
				}
			});
			// CodeBlockIDEEXE.run(52000);
			for (int i = 50; i > 0; i--) {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				if (testsocket == null)
					continue;
				try {
					// socket.writeEnd("f611391e32884fa69fc653133f250a14000000ER-System.Err.md5");
					testsocket.writeEnd(msg("000001", "IDE", "-v"));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			try {
				Thread.sleep(1000 * 1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("开始关闭");
			serverSocket.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	
	}

	public static void main(String[] args) {
		CodeBlockScriptEngineManager codeBlockScriptEngineManager = new CodeBlockScriptEngineManager();
		/*Test test = new Test();
		test.run();*/
		ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
		ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("js");
		// ScriptEngineBuffer.upgradeScriptEngine(scriptEngine);
		try {
			scriptEngine.put("console", System.out);
			scriptEngine.eval("var System = Java.type('java.lang.System');"
					+ "console.println('');");
			System.currentTimeMillis();
		} catch (ScriptException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static String msg(String msgId, String head, String data) {
		String str = msgId + head + data;
		return MD5.encoderMd5(str) + str;
	}

}
